What's the difference between the [OptionalField] and [NonSerialized]

Posted by IbrarMumtaz on Stack Overflow See other posts from Stack Overflow or by IbrarMumtaz
Published on 2010-04-06T16:26:08Z Indexed on 2010/04/06 16:43 UTC
Read the original article Hit count: 201

I came across this question on transcender:

What should you apply to a field if its value is not required during deserialization?

Me = [NonSerialized], ANSWER = [OptionalField]

My gut reaction was NonSerialised, I have no idea why but in the space of 5 seconds thats what I thought but to my surprise, Transcender says I am wrong. OK fair enough .... but why? looking more closely at the question I have a good idea what to look out for as far as the [Nonseralized] attribute is concerned but still I would really like this clearing up.

As far as I can tell the former has relationship with versioning conflicts between newer and older versions of the same assembly. The later is more concerned with not serializing a field FULLSTOP. Is there anything else that might pick these two apart? MSDN does not really say much about this as they both are used on the BinaryFormatters and SoapFormatter with the XMLFormatter using the XMLIgnoreAttribute.

My second question is can you mix and match either one of the two attributes ... I am yet to use them as I have not had an excuse to mess about with them. So my curiosity can only go so far.

Just throwing this one out there, but does my answer have something to do with the way [OnDeserialized] and the IdeserilizationCallback interface is implemented???? Am guessing here ....

Thanks In Advance

UPDATE:

I know that optional field attribute does not serialize the value held by a data member but NonSerialized will not even serialise the data member or its value. That sounds about a right???? That's all I got on these two attributes.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#